<?php

/*-----------------------------------------------------------*\
|                 - Модуль матеріалів -                      |
|                 Author:  Skalden Russell                   |
|                 Last modify: 20130105                      |
\*-----------------------------------------------------------*/

// Захист скрипта від довільного запуску
if(!defined('TSUE_SEC')) {
	header('Content-Type: text/html; charset=utf-8');
	@require_once( '../../data/messages/hacking.attempt.php' );
	die( $TSUE_MSG );
}


define( "FILE_PER_PAGE", $MALINA_CONF['file_pp'] );
define( "PUBLIC_PER_PAGE", $MALINA_CONF['public_pp'] );


	// Кодування / декодування тексту
	$sectext = new CSecurity();
	
	// База Даних
	$sql = new cMysql();
	$sql->connect($SQL_CONF['host'], $SQL_CONF['user'], $SQL_CONF['pass'], $SQL_CONF['dbname']);
	$file_count = $sql->fetch_assoc("SELECT COUNT(*) as count FROM `". $SQL_CONF['pref'] ."materialfile`");
	$mat_count = $sql->fetch_assoc("SELECT COUNT(*) as count FROM `". $SQL_CONF['pref'] ."materialpublic`");
	
	$fpage = $sql->sec_check( $sectext->digit( $_GET['fpage'] ) );
	$ppage = $sql->sec_check( $sectext->digit( $_GET['ppage'] ) );
	$file_splitter = make_splitter( $fpage, $file_count[0]['count'], FILE_PER_PAGE, '?p=materials&ppage='. $ppage .'&fpage=' );
	$mat_splitter = make_splitter( $ppage, $mat_count[0]['count'], PUBLIC_PER_PAGE, '?p=materials&fpage='. $fpage .'&ppage=' );
	
	// Запит файлів
	$file_rows = $sql->fetch_array("SELECT `materialid`, `title`, `filename`, `description`, `authors`, `views`, `date`  FROM `" . $SQL_CONF['pref'] ."materialfile` LIMIT " . $file_splitter['begin'] . ", " . FILE_PER_PAGE . " ");
	// Запит матеріалів
	$mat_rows = $sql->fetch_array("SELECT `materialid`, `title`, `pagedescr`, `author`, `views`, `date` FROM `" . $SQL_CONF['pref'] ."materialpublic` LIMIT " . $mat_splitter['begin'] . ", " . PUBLIC_PER_PAGE . " ");
	$sql->close();
	
	// Контент статей
	$TSUE_CONTENT = "<br /><h1>Статті</h1><br />";
	$mat_size = count( $mat_rows ) - 1;
	if( $mat_size == 0 )
	{
		$TSUE_CONTENT .= "Матеріали відсутні";
	} 
	else 
	{
		for($i = 0; $i < $mat_size; $i++)
		{
			$pContent->SetReplace( "materialid",	$sectext->digit( $mat_rows[$i]['materialid'] ) );
			$pContent->SetReplace( "title",		$sectext->decode( $mat_rows[$i]['title'] ) );
			$pContent->SetReplace( "description",	$sectext->decode( $mat_rows[$i]['pagedescr'] ) );
			$pContent->SetReplace( "author",	$sectext->decode( $mat_rows[$i]['author'] ) );
			$pContent->SetReplace( "views",		$sectext->digit( $mat_rows[$i]['views'] ) );
			$pContent->SetReplace( "date",		$sectext->decode( $mat_rows[$i]['date'] ) );
			
			$TSUE_CONTENT .= $pContent->getContent( 'styles/' . $MALINA_CONF['theme_cur'] . '/table_material.html', 'r' );
		}
	}
	
	
	$TSUE_CONTENT .= $mat_splitter['splitter'];
	
	// Контент файлів
	$TSUE_CONTENT .= "<br /><h1>Файлы</h1><br />";
	$file_size = count( $file_rows ) - 1;
	if($file_rows == 0)
	{
		$TSUE_CONTENT .= "файли відсутні";
	}
	else
	{
		for($i = 0; $i < $file_size; $i++)
		{
			$pContent->SetReplace( "materialid",	$sectext->digit( $file_rows[$i]['materialid'] ) );
			$pContent->SetReplace( "title",		$sectext->decode( $file_rows[$i]['title'] ) );
			$pContent->SetReplace( "filename",	$sectext->decode( $file_rows[$i]['filename'] ) );
			$pContent->SetReplace( "description",	$sectext->decode( $file_rows[$i]['description'] ) );
			$pContent->SetReplace( "author",	$sectext->decode( $file_rows[$i]['authors'] ) );
			$pContent->SetReplace( "views",		$sectext->digit( $file_rows[$i]['views'] ) );
			$pContent->SetReplace( "date",		$sectext->decode( $file_rows[$i]['date'] ) );
			
			$TSUE_CONTENT .= $pContent->getContent( 'styles/' . $MALINA_CONF['theme_cur'] . '/table_files.html', 'r' );
		}
	}
	
	$TSUE_CONTENT .= $file_splitter['splitter'];
	

?>
